﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using App.Core.Extensions;
using App.Data;
using App.Core.DB;

public partial class Pages_Public_ShowNews : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindNewsList(1);
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void BindNewsList(int pageNumber)
    {
        int totalRecord = 0;
        UtilityDAO dao = new UtilityDAO();
        DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_NEWS_LIST, null, pageNumber, int.MaxValue, out totalRecord);
        //ucNewsList.DataSource = ds.Tables[0];
        //ucNewsList.EditLink = "/Pages/Admin/NewsEdit.aspx?" + AppConstants.QueryString.ID + "={0}";
        //ucNewsList.DeleteLink = Request.Url.AbsolutePath + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.DELETE + "=True";
        //ucNewsList.DataBind();

        String listItemHtml = @"<div class='ListItem'>                                    
                                    <div class='ListTitle'><a href='/Pages/Public/NewsDetails.aspx?ID={0}'>{1}</a></div>
                                    <div class='ListSubTitle'><b>Published On:</b> {2}</div>
                                    <div style='margin-top:5px;'>
                                        {3}                                                        
                                    </div>                                    
                                </div>";
        //String thumbImage = "<img src='" + AppConstants.PROJECT_FILES + "/{0}/{1}' alt='' title='' align='right' class='ThumbInList' />";
        StringBuilder sb = new StringBuilder(10);
        //foreach (Project project in projects)
        foreach(DataRow row in ds.Tables[0].Rows)
        {
            int newsID = NullHandler.GetInt(row["ID"]);
            sb.AppendFormat(listItemHtml, newsID, row["Title"]
                , NullHandler.GetDateTime(row["PublishedOn"]).ToString(AppConstants.ValueOf.DATE_FROMAT_DISPLAY)
                , GetDisplayableNewsContent(NullHandler.GetString(row["News"]), newsID));
            //String priceHtml = String.Empty;
            //String thumnail = String.Empty;
            //if (project.PricePerUnitFrom > 0 && project.PricePerUnitTo > 0 & project.ShowPricingInfo)
            //    priceHtml = String.Format("From {0} To {1}", WebUtil.FormatData(project.PricePerUnitFrom), WebUtil.FormatData(project.PricePerUnitTo));
            //else
            //    priceHtml = "Negotiatable";

            //ProjectFile projectFile = project.ProjectFiles.FirstOrDefault(P => P.FileTypeID != ConfigReader.ProjectBrocheureFileTypeID);
            //if (projectFile != null && !projectFile.ThumbFileName.IsNullOrEmpty())
            //    thumnail = String.Format(thumbImage, project.ID, projectFile.ThumbFileName);

            //sb.AppendFormat(listItemHtml, project.ID, project.Name.HtmlEncode(),
            //    String.Format("{0}, {1}", project.Zone.City.Name, project.Zone.Name),
            //    project.Unit.Name, priceHtml,
            //    thumnail, GetDisplayableProjectDescription(project),
            //    project.Customer.Name.HtmlEncode());
        }
        ltrListing.Text = sb.ToString();
    }
    protected String GetDisplayableNewsContent(String text, int contentID)
    {
        if (text.WordCount() > ConfigReader.ProjectListDescriptionWordCount)
            return String.Format("{0} &nbsp;&nbsp;<a href='/Pages/Public/NewsDetails.aspx?ID={1}'>Details</a>", WebUtil.FormatText(text.GetWords(ConfigReader.ProjectListDescriptionWordCount)), contentID);

        return WebUtil.FormatText(text);

    }
}